package com.cheerscpl.service.impl;

import com.cheerscpl.Task;
import com.cheerscpl.TaskQueue;
import com.cheerscpl.TaskQueue4Redis;
import com.cheerscpl.protocol.model.DataPacket;
import com.cheerscpl.service.TaskQueueService;
import com.cheerscpl.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * Created by chenpuliang on 2017/6/4.
 */
@Service
public class TaskServiceImpl implements TaskService{

    @Autowired
    private TaskQueueService taskQueueService;

    @Override
    public void handleTasks(TaskQueue taskQueue) {
        if(taskQueue != null){
            Task task = taskQueue.popTask();
            while(task != null){
                task.send();
                task = taskQueue.popTask();
            }
        }
    }

    @Override
    public void addTask(Task task) {
        TaskQueue taskQueue = taskQueueService.getTaskQueue(task.getName());
        if(taskQueue != null){
            taskQueue.pushTask(task);
        }else {
            taskQueue = new TaskQueue4Redis(task.getName());
            taskQueue.pushTask(task);
            taskQueueService.addQueue(taskQueue);
        }
    }
}